﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MiniProject.Models;
using Xamarin.Forms.Extended;

namespace MiniProject.ViewModels
{
    //由齐雨泽完成

    /// <summary>
    /// 搜索结果页ViewModel
    /// </summary>
    public class ResultPageViewModel:ViewModelBase {
        //todo 供演示
        private IFundStorage _fundStorage;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="fundStorage">基金存储</param>
        public ResultPageViewModel(IFundStorage fundStorage) {
            //todo 供演示
            _fundStorage = fundStorage;

            FundCollection = new InfiniteScrollCollection<Fund> {
                OnCanLoadMore = ()=> _canLoadMore,
                OnLoadMore = async () => {
                    Status = Loading;
                    var funds = await fundStorage.GetFundsAsync(Where,FundCollection.Count,PageSize);

                    Status = "";

                    if (funds.Count < PageSize) {
                        _canLoadMore = false;
                        Status = NoMoreResult;
                    }

                    if (funds.Count == 0 && FundCollection.Count == 0) {
                        Status = NoResult;
                    }

                    return funds;
                }
            };
        }

        /// <summary>
        /// 基金集合
        /// </summary>
        public InfiniteScrollCollection<Fund> FundCollection { get; }

        private string _status;

        public string Status {
            get => _status;
            set => Set(nameof(Status), ref _status, value);
        }

        private Expression<Func<Fund, bool>> _where;

        public Expression<Func<Fund,bool>> Where
        {
            get => _where;
            set {
                Set(nameof(Where), ref _where, value);
                _isNewQuery = true;
                
            }
        }

        private RelayCommand _pageApperingCommand;

        public RelayCommand PageAppearingCommand => _pageApperingCommand ?? (_pageApperingCommand = new RelayCommand(
            async () => PageAppearingCommandFunction()
            ));

        public async Task PageAppearingCommandFunction() {
            //todo 供演示使用，后来应删除
            Where = Expression.Lambda<Func<Fund, bool>>(
                Expression.Constant(true),
                Expression.Parameter(typeof(Fund), "p"));

            //todo 供演示使用，后来应删除
            await _fundStorage.InitializeAsync();

            if (!_isNewQuery)
            {
                return;
            }

            _isNewQuery = false;

            FundCollection.Clear();
            _canLoadMore = true;
            await FundCollection.LoadMoreAsync();
        }

        public const int PageSize = 20;

        public const string Loading = "正在载入";

        public const string NoResult = "没有满足条件的结果";

        public const string NoMoreResult = "没有更多结果";

        public bool _canLoadMore;

        public bool _isNewQuery;
    }
}
